from django.conf import settings
from django.forms.models import model_to_dict
from django.db.models import Avg, Sum, Count, Max, Min, Q, F
from django_filters import *
from django_filters.rest_framework import DjangoFilterBackend, FilterSet

from rest_framework import viewsets
from rest_framework import serializers
from rest_framework import versioning
from rest_framework.response import Response
from rest_framework.serializers import *
from rest_framework.validators import UniqueValidator, UniqueTogetherValidator  # 验证器
from rest_framework.filters import SearchFilter, OrderingFilter

from iClouds.apidev import filter_views
from iClouds.Permissions import (
    OR,
    AllPermissions,
    MyPermissions,
    BoxPermissions,
    WeChatPermissions,
    BarPermissions,
)
from iClouds.Authentication import (
    MyJWTAuthentication,
    BoxJWTAuthentication,
    WeChatJWTAuthentication,
    BarJWTAuthentication,
)

from mysqldb.models import *
from mysqldb.serializers import ListDjangoFilterBackend


class NoneSerializer(serializers.Serializer):
    """无序列化"""

    class Meta:
        fields = "__all__"


class AllinpayHardwareListSerializer(serializers.ModelSerializer):
    """资产列表"""

    id = IntegerField(label="ID", read_only=True)
    area = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="区域",
    )
    pcname = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="主机名",
    )
    ipaddr = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="IP地址",
    )
    mac = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="mac地址",
    )
    board = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="主板",
    )
    cpu = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="CPU",
    )
    memory = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="内存",
    )
    video = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="显卡",
    )
    display = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="显示器",
    )
    netadapter = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="网卡",
    )
    network = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="网络连接",
    )
    mouse = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="鼠标",
    )
    keyboard = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="键盘",
    )
    headset = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="音频设备",
    )
    cpu_temperature = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="cpu温度",
    )
    board_temperature = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="主板温度",
    )
    video_temperature = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="显卡温度",
    )
    speed = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="网卡速率",
    )
    status = IntegerField(
        allow_null=True,
        max_value=2147483647,
        min_value=-2147483648,
        required=False,
        help_text="状态",
    )
    lasttime = DateTimeField(read_only=True, help_text="最近同步时间")
    pid = IntegerField(help_text="场所ID")

    class Meta:
        model = AllinpayHardwareList
        fields = "__all__"


class AllinpayHardwareConfSerializer(serializers.ModelSerializer):
    """资产配置"""

    id = IntegerField(label="ID", read_only=True)
    name = ChoiceField(
        choices=(
            ("area", "区域变更报警"),
            ("ipaddr", "ip地址变更报警"),
            ("mac", "MAC变更报警"),
            ("board", "主板变更报警"),
            ("cpu", "CPU变更报警"),
            ("memory", "内存变更报警"),
            ("video", "显卡变更报警"),
            ("display", "显示器变更报警"),
            ("netadapter", "网卡变更报警"),
            ("network", "网络连接变更报警"),
            ("mouse", "鼠标变更报警"),
            ("keyboard", "键盘变更报警"),
            ("headset", "音频设备变更报警"),
            ("cpu_temperature", "CPU温度阈值报警"),
            ("board_temperature", "主板温度阈值报警"),
            ("video_temperature", "显卡温度阈值报警"),
            ("speed", "网卡速率阈值报警"),
        ),
        help_text="配置项",
    )
    compare = ChoiceField(
        choices=(
            ("exact", "等于"),
            ("gt", "大于"),
            ("gte", "大于等于"),
            ("lt", "小于"),
            ("lte", "小于等于"),
            ("ne", "不等于"),
            ("contains", "包含"),
            ("isnull", "是否为null"),
            ("equal", "字符串变更"),
        ),
        help_text="运算符",
    )
    value = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="阈值",
    )
    pid = IntegerField(allow_null=True, required=True, help_text="场所ID")
    status = IntegerField(min_value=0, max_value=1, help_text="状态")
    remarks = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="备注",
    )

    class Meta:
        model = AllinpayHardwareConf
        fields = "__all__"
        validators = [
            UniqueTogetherValidator(
                queryset=AllinpayHardwareConf.objects.all(), fields=("name", "pid")
            )
        ]


class FsygWorkLoginSerializer(serializers.Serializer):
    """工作站登录"""

    wbid = CharField(
        allow_blank=True, allow_null=True, max_length=255, help_text="凡商编号"
    )
    computer_name = CharField(
        allow_blank=True, allow_null=True, max_length=255, help_text="终端机号"
    )

    class Meta:
        fields = "__all__"


class FsygLineinfoListSerializer(serializers.ModelSerializer):
    """上机在线信息序列化"""

    class Meta:
        model = FsygLineinfoList
        fields = "__all__"


# print(repr(AllinpayHardwareConfSerializer()))
